package com.yycwxt.modules.admin.shop;

import com.yycwxt.common.ReturnResult;
import com.yycwxt.domain.Shop;
import com.yycwxt.modules.admin.store.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@Api
@RestController
@Validated
@RequestMapping("/shops")
public class ShopController {


    private ShopService shopService;
    @Autowired
    public ShopController( ShopService shopService) {
        this.shopService = shopService;
    }

    @GetMapping()//get  /shops
    @ApiOperation("查询全部店铺")
    public ReturnResult selectAll(){
        return ReturnResult.buildSuccessResult(shopService.list());
    }

    @GetMapping("/{id}")//get  /shops/1
    @ApiOperation("查询店铺")
    public ReturnResult getById(@PathVariable Integer id){
        return ReturnResult.buildSuccessResult(shopService.getById(id));
    }

//    @GetMapping("/findByName")
//    @ApiOperation("shop表根据店铺名查询表")
//    public List<Shop> selectByName(@ApiParam(name = "input输入值", required = true, type = "String") @NotNull String name){
//        return shopService.selectByName(name);
//    }

    @PostMapping()//post  /shops
    @ApiOperation("新增店铺")
    public ReturnResult addShop(@ApiParam(name = "shop对象",required = true,type = "shop") @RequestBody @Validated Shop shop){
        try {
            return ReturnResult.buildSuccessResult(shopService.save(shop));
        } catch (DuplicateKeyException dke) {
            return ReturnResult.buildFailureResult("id/code/name 值重复或者存在必须属性为空");
        } catch (Exception e) {
            return ReturnResult.buildFailureResult("参数错误");
        }
    }

    @DeleteMapping("/{id}")//delete  /shops/1
    @ApiOperation("shop表根据id删除店铺")
    public ReturnResult deleteShop(@ApiParam(name = "ID值",required = true,type = "int") @PathVariable Integer id){
        return ReturnResult.buildSuccessResult(shopService.removeById(id));
    }

    @PutMapping()//put  /shops/1
    @ApiOperation("shop表根据id更改店铺")
    public ReturnResult updateShop(@Validated @ApiParam(name = "shop对象",required = true,type = "shop") @RequestBody Shop shop){
        return ReturnResult.buildSuccessResult(shopService.updateById(shop));
    }


    @PostMapping("/upload")
    @ApiOperation("Excel导入数据")
    public ReturnResult excelToDatabase(@ApiParam(value = "上传文件", type = "_file") @RequestPart("file") MultipartFile file) {
        Result result = new Result();
        if(shopService.importFileToDB(file)){
            return ReturnResult.buildSuccessResult("添加成功",null);
        }else {
            return ReturnResult.buildFailureResult("添加失败",null);
        }
    }

    @GetMapping("/selectByInput")
    @ApiOperation("根据输入条件进行全文检索")
    public ReturnResult selectByInput(@ApiParam(name = "input检索内容", required = true, type = "String") String input){
        return ReturnResult.buildSuccessResult(shopService.selectByInput(input));
    }
}
